﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace GJLib.inernal
{
    public class 盤位
    {
        public int id;
        
        public String 地 { get; set; }
        public String 天將 { get; set; }
        public String 天 { get; set; }
        public String 支神 { get; set; }
        public 盤位()
        {
            地 = "";
            天 = "";
            天將 = "";
            支神 = "";
        }
    }
    public class Trans
    {
        [System.ComponentModel.DefaultValue("")]
        public String 支神 { get; set; }
        [System.ComponentModel.DefaultValue("")]
        public String 遁干 { get; set; }
        [System.ComponentModel.DefaultValue("")]
        public String 天將 { get; set; }
        [System.ComponentModel.DefaultValue("")]
        public String 六親 { get; set; }
        [JsonIgnore]
        public bool 空亡 = false;
    }
    public class Trans3
    {
        public Trans 初傳 { get; set; }
        public Trans 中傳 { get; set; }
        public Trans 末傳 { get; set; }
        public Trans3()
        {
            初傳 = new Trans();
            中傳 = new Trans();
            末傳 = new Trans();
        }
    }
    public enum 賊剋
    {
        None = 0,
        賊 = -1,
        剋 = 1
    };
    public class Clazz
    {
        [JsonIgnore]
        public String 上神 { get { return content.Substring(0, 1); } }
        [JsonIgnore]
        public String 下神 { get { return content.Substring(1, 1); } }
        [JsonIgnore]
        public 賊剋 剋 = 賊剋.None;
        public String 柱 { get { return content; } }
        public String 天將;

        protected String content;
        [JsonIgnore]
        public int 上神孟仲季
        {
            get
            {
                return GJR.孟仲季(上神);
            }
        }
        public Clazz(String c)
        {
            content = c;
            int k = GJR.A剋B(上神, 下神);
            if (k == 0)
                剋 = 賊剋.None;
            if (k > 0)
                剋 = 賊剋.剋;
            if (k < 0)
                剋 = 賊剋.賊;
        }
    }
    public class BigSixNinePan
    {
        public DateTime 日期;
        public Dictionary<String, Trans> 三傳 = null;
        public Clazz[] 課;
        public 盤位[] 盤;
        public String 月將;
        public Pillar4 占課四柱 = null;
        public Dictionary<String, Object> options = new Dictionary<string, object>();
        protected List<String> _transStep = new List<string>();
        public int 發用課 = -1;
        private string _貴人順逆 = "";
        protected Dictionary<String, String> _天將五行 = null;

        private string _行年 = "";
        public string 行年
        {
            get
            {
                return _行年;
            }
            set
            {
                _行年 = value;
            }
        }
        public string 貴人順逆
        {
            get
            {
                return _貴人順逆;
            }
            set
            {
                _貴人順逆 = value;
            }
        }
        public Dictionary<String, String> 天將五行
        {
            get
            {
                return GJR.天將五行();
            }
        }
        public List<String> transStep
        {
            get { return _transStep; }
            set { _transStep = value; }
        }
        public BigSixNinePan()
        {
            三傳 = new Dictionary<string, Trans>();
            三傳.Add("初傳", new Trans());
            三傳.Add("中傳", new Trans());
            三傳.Add("末傳", new Trans());
        }
        public String 取上神(String ji)
        {
            bool isji = GJR.isJi(ji);
            if (isji == false)
            {
                ji = GJR.天干寄宮(ji);
            }
            foreach (var item in 盤)
            {
                if (item.地.Equals(ji))
                    return item.天;
            }
            return "";
        }
        public String 取下神(String ji)
        {
            foreach (var item in 盤)
            {
                if (item.天.Equals(ji))
                    return item.地;
            }
            return "";
        }
        public 盤位 地盤(String ji)
        {
            foreach (var item in 盤)
            {
                if (item.地.Equals(ji))
                    return item;
            }
            return null;
        }

        public 盤位 天盤(String ji)
        {
            foreach (var item in 盤)
            {
                if (item.天.Equals(ji))
                    return item;
            }
            return null;
        }

        public bool is14同()
        {
            if (課[0].上神.Equals(課[3].上神) && GJR.天干寄宮(課[0].下神).Equals(課[3].下神))
                return true;
            return false;
        }
        public bool is23同()
        {
            if (課[1].上神.Equals(課[2].上神) && 課[1].下神.Equals(課[2].下神))
                return true;
            return false;
        }
        public bool 不備
        {
            get
            {
                if (八專)
                    return true;
                if (課[1].上神.Equals(課[2].上神) && 課[1].下神.Equals(課[2].下神))
                    return true;
                if (課[0].上神.Equals(課[3].上神) && GJR.天干寄宮(課[0].下神).Equals(課[3].下神))
                    return true;
                return false;
            }
        }
        public bool 八專
        {
            get
            {
                return GJR.天干寄宮(日干).Equals(日支);
            }
        }
        public bool 伏吟
        {
            get
            {
                return "子".Equals(盤[1].天);
                //return  占課四柱.hourGj.支.Equals(月將);
            }
        }
        public bool 返吟
        {
            get
            {
                return "午".Equals(盤[1].天);
            }
        }

        public String 日干
        {
            get { return 占課四柱.dayGj.干; }
            set
            {
                if (占課四柱 == null)
                    占課四柱 = new Pillar4();
                占課四柱.dayGj.干 = value;
            }
        }
        public String 日干支
        {
            get { return 占課四柱.dayGj.干支; }
            set
            {
                if (占課四柱 == null)
                    占課四柱 = new Pillar4();
                占課四柱.dayGj.干支 = value;
            }
        }
        public String 日支
        {
            get { return 占課四柱.dayGj.支; }
            set
            {
                if (占課四柱 == null)
                    占課四柱 = new Pillar4();
                占課四柱.dayGj.支 = value;

            }
        }
        public String 時支
        {
            get { return 占課四柱.hourGj.支; }
            set
            {
                if (占課四柱 == null)
                    占課四柱 = new Pillar4();
                占課四柱.hourGj.支 = value;
            }
        }


        public void setPillar(Pillar4 s)
        {
            占課四柱 = s.clone();
        }
        public void setClass4(List<String> c4)
        {
            課 = new Clazz[4];
            for (int i = 0; i < 4; i++)
            {
                課[i] = new Clazz(c4[i]);
                課[i].天將 = 天盤(課[i].上神).天將;
            }
        }
        public void setKeyPan(List<String> pan)
        {
            if (盤 == null)
            {
                盤 = new 盤位[12];
                for (int i = 0; i < 12; i++)
                {
                    盤位 p = new 盤位();
                    p.地 = GJR.Id2Ji(i);
                    盤[i] = p;
                }
            }
            for (int i = 0; i < 12; i++)
            {
                盤[i].天 = pan[i];
                盤[i].支神 = GJR.支神(盤[i].天);
            }
        }
        public void setPan(List<Dictionary<string, string>> pan)
        {
            盤位[] ps = new 盤位[12];
            int sz = pan.Count;
            for (int i = 0; i < sz; i++)
            {
                盤位 p = new 盤位();
                p.天 = pan[i]["天"];
                p.天將 = pan[i]["天將"];
                p.地 = pan[i]["地"];
                p.支神 = GJR.支神(p.天);
                p.id = i;
                ps[i] = p;
            }
            盤 = ps;
        }
        public Dictionary<String, String> getPos(String Ji)
        {
            return null;
        }
        public bool has賊剋()
        {
            return (賊課().Count + 剋課().Count) > 0;
        }
        public List<int> 賊課()
        {
            List<int> 賊課 = new List<int>();
            for (int i = 0; i < 4; i++)
            {
                if (課[i].剋 == 賊剋.賊)
                    賊課.Add(i);
            }
            return 賊課;
        }
        public List<int> 剋課()
        {
            List<int> 剋課 = new List<int>();
            for (int i = 0; i < 4; i++)
            {
                if (課[i].剋 == 賊剋.剋)
                    剋課.Add(i);
            }
            return 剋課;
        }
        public void addTransNote(String p)
        {
            transStep.Add(p);
        }
        public String getTransNode()
        {
            return String.Join(",", transStep);
        }
        public void Dump()
        {

            foreach (var item in 盤)
            {
                String p = item.地 + " " + item.天 + " " + item.天將;
                System.Diagnostics.Debug.WriteLine(p);
            }

            String l0 = this.課[3].天將.Substring(0, 1) + this.課[2].天將.Substring(0, 1) + this.課[1].天將.Substring(0, 1) + this.課[0].天將.Substring(0, 1);
            String l1 = this.課[3].上神 + this.課[2].上神 + this.課[1].上神 + this.課[0].上神;
            String l2 = this.課[3].下神 + this.課[2].下神 + this.課[1].下神 + this.課[0].下神;
            System.Diagnostics.Debug.WriteLine(l0);
            System.Diagnostics.Debug.WriteLine(l1);
            System.Diagnostics.Debug.WriteLine(l2);
            foreach (var 傳 in this.三傳.Values)
            {
                System.Diagnostics.Debug.WriteLine(傳.六親 + "|" +
                                                                                   傳.遁干 + "|" +
                                                                                   傳.支神 + "|" +
                                                                                   傳.天將);
            }



        }

        public string toJson()
        {
            return JsonConvert.SerializeObject(this, Formatting.Indented);
        }

        public string get三傳()
        {
            return
            三傳["初傳"].支神 +
                三傳["中傳"].支神 +
                三傳["末傳"].支神;
        }
        public bool is24同()
        {
            if (課[1].上神.Equals(課[3].上神) && 課[1].下神.Equals(課[3].下神))
                return true;
            return false;
        }
        public enum 盤項
        {
            盤課傳=1, 含天將=2
        }
        public bool isEqual(BigSixNinePan other,盤項 items)
        {
            return true;
        }
    }
}
